﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CSTest.Format
{
    class DateTimeFormatTest
    {
        public void TestFormat()
        {
            DateTime now = DateTime.Now;
            Console.WriteLine(now);
            Console.WriteLine(now.ToString().ToLowerInvariant());
            Console.WriteLine(now.ToString("Y"));
            Console.WriteLine(now.ToString((new CultureInfo("zh-CN")).DateTimeFormat.ShortDatePattern));
            Console.WriteLine(now.ToString((new CultureInfo("en-US")).DateTimeFormat.ShortDatePattern));
        }

        public static void Test()
        {
            string msgShortDate = "(d) Short date: . . . . . . . ";
            string msgLongDate = "(D) Long date:. . . . . . . . ";
            string msgShortTime = "(t) Short time: . . . . . . . ";
            string msgLongTime = "(T) Long time:. . . . . . . . ";
            string msgFullDateShortTime =
                                  "(f) Full date/short time: . . ";
            string msgFullDateLongTime =
                                  "(F) Full date/long time:. . . ";
            string msgGeneralDateShortTime =
                                  "(g) General date/short time:. ";
            string msgGeneralDateLongTime =
                                  "(G) General date/long time (default):\n" +
                                  "    . . . . . . . . . . . . . ";
            string msgMonth = "(M) Month:. . . . . . . . . . ";
            string msgRFC1123 = "(R) RFC1123:. . . . . . . . . ";
            string msgSortable = "(s) Sortable: . . . . . . . . ";
            string msgUniSortInvariant =
                                  "(u) Universal sortable (invariant):\n" +
                                  "    . . . . . . . . . . . . . ";
            string msgUniSort = "(U) Universal sortable: . . . ";
            string msgYear = "(Y) Year: . . . . . . . . . . ";
            string msgRoundtripLocal = "(o) Roundtrip (local):. . . . ";
            string msgRoundtripUTC = "(o) Roundtrip (UTC):. . . . . ";
            string msgRoundtripUnspecified = "(o) Roundtrip (Unspecified):. ";


            string msg1 = "Use ToString(String) and the current thread culture.\n";
            string msg2 = "Use ToString(String, IFormatProvider) and a specified culture.\n";
            string msgCulture = "Culture:";
            string msgThisDate = "This date and time: {0}\n";

            DateTime thisDate = DateTime.Now;
            DateTime utcDate = thisDate.ToUniversalTime();
            DateTime unspecifiedDate = new DateTime(2000, 3, 20, 13, 2, 3, 0, DateTimeKind.Unspecified);
            CultureInfo ci;

            // Format the current date and time in various ways.
            //Console.Clear();
            Console.WriteLine("Standard DateTime Format Specifiers:\n");
            Console.WriteLine(msgThisDate, thisDate);
            Console.WriteLine(msg1);

            // Display the thread current culture, which is used to format the values.
            ci = Thread.CurrentThread.CurrentCulture;
            Console.WriteLine("{0,-30}{1}\n", msgCulture, ci.DisplayName);

            Console.WriteLine(msgShortDate + thisDate.ToString("d"));
            Console.WriteLine(msgLongDate + thisDate.ToString("D"));
            Console.WriteLine(msgShortTime + thisDate.ToString("t"));
            Console.WriteLine(msgLongTime + thisDate.ToString("T"));
            Console.WriteLine(msgFullDateShortTime + thisDate.ToString("f"));
            Console.WriteLine(msgFullDateLongTime + thisDate.ToString("F"));
            Console.WriteLine(msgGeneralDateShortTime + thisDate.ToString("g"));
            Console.WriteLine(msgGeneralDateLongTime + thisDate.ToString("G"));
            Console.WriteLine(msgMonth + thisDate.ToString("M"));
            Console.WriteLine(msgRFC1123 + utcDate.ToString("R"));
            Console.WriteLine(msgSortable + thisDate.ToString("s"));
            Console.WriteLine(msgUniSortInvariant + utcDate.ToString("u"));
            Console.WriteLine(msgUniSort + thisDate.ToString("U"));
            Console.WriteLine(msgYear + thisDate.ToString("Y"));
            Console.WriteLine(msgRoundtripLocal + thisDate.ToString("o"));
            Console.WriteLine(msgRoundtripUTC + utcDate.ToString("o"));
            Console.WriteLine(msgRoundtripUnspecified + unspecifiedDate.ToString("o"));

            Console.WriteLine();

            // Display the same values using a CultureInfo object. The CultureInfo class 
            // implements IFormatProvider.
            Console.WriteLine(msg2);

            // Display the culture used to format the values. 
            ci = new CultureInfo("de-DE");
            Console.WriteLine("{0,-30}{1}\n", msgCulture, ci.DisplayName);

            Console.WriteLine(msgShortDate + thisDate.ToString("d", ci));
            Console.WriteLine(msgLongDate + thisDate.ToString("D", ci));
            Console.WriteLine(msgShortTime + thisDate.ToString("t", ci));
            Console.WriteLine(msgLongTime + thisDate.ToString("T", ci));
            Console.WriteLine(msgFullDateShortTime + thisDate.ToString("f", ci));
            Console.WriteLine(msgFullDateLongTime + thisDate.ToString("F", ci));
            Console.WriteLine(msgGeneralDateShortTime + thisDate.ToString("g", ci));
            Console.WriteLine(msgGeneralDateLongTime + thisDate.ToString("G", ci));
            Console.WriteLine(msgMonth + thisDate.ToString("M", ci));
            Console.WriteLine(msgRFC1123 + utcDate.ToString("R", ci));
            Console.WriteLine(msgSortable + thisDate.ToString("s", ci));
            Console.WriteLine(msgUniSortInvariant + utcDate.ToString("u", ci));
            Console.WriteLine(msgUniSort + thisDate.ToString("U", ci));
            Console.WriteLine(msgYear + thisDate.ToString("Y", ci));
            Console.WriteLine(msgRoundtripLocal + thisDate.ToString("o", ci));
            Console.WriteLine(msgRoundtripUTC + utcDate.ToString("o", ci));
            Console.WriteLine(msgRoundtripUnspecified + unspecifiedDate.ToString("o", ci));

            Console.WriteLine();
        }
    }
}
